<?php

namespace app\api\controller;

use think\Db;
use app\data\Data;
use app\api\model\Form as order_list;
use app\api\model\Preg;

class Orders extends Index
{


    /**
     * 交易方式查询
     */
    public function get_orders()
    {
        if ($_GET['post'] == "service") {
            $where['level'] = 1;
            $res = Db::name('service_type')->where($where)->select();
        } else if ($_GET['post'] == "trading") {
            $res = db('market_trading_cid')->select();
        }
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '成功', $res);

        } else {
            $this->_jsonp('1000', '失败', null);
        }
    }


    /**
     * 显示资源列表
     *宝贝管理
     * @return \think\Response
     */
    public function orders()
    {
        if ($_GET['post'] == "service") {
            $where['services'] = 2;
        } else if ($_GET['post'] == "trading") {
            $where['services'] = 1;
        }
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();

        $where['a.time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');


        if ($where['services'] == 1) {
            $static = (isset($_GET['static']) && $_GET['static']) ? $_GET['static'] : 0;
            if ($static == 1) {
                $where['a.status'] = 2;
                $where['a.static'] = 1;
            } else if ($static == 2) {
                $where['a.static'] = 2;
            } else if ($static == 3) {
                $where['a.status'] = 7;
                $where['a.static'] = 1;
            } else if ($static == 4) {
                $where['a.status'] = 9;
                $where['a.static'] = 4;
            } else if ($static == 5) {
                $where['a.status'] = array('in', array(10, 11, 12, 13));
                $where['a.static'] = array('in', array(1, 3));
            } else if ($static == 6) {
                $where['a.status'] = array('in', array(3, 4, 5, 6, 8));
                $where['a.static'] = 1;
            }
            $where['m.c_id'] = (isset($_GET['transaction_id']) && !empty($_GET['transaction_id'])) ? $_GET['transaction_id'] : array('neq', 0);
            $where['a.order|m.username'] = array('like', "%$username%");
            $in[0] = "__USER__ u";
            $in[1] = "u.uid = a.username";
            $ji[0] = "__MARKET_TRADING__ m";
            $ji[1] = "m.id = a.name";
            $pn[0] = "__MARKET_TRADING_CID__ c";
            $pn[1] = "c.id = m.c_id";
            $join[0] = "__SHOP_INFORMATION__ s";
            $join[1] = "s.id = m.name";
            $field = 'a.id,a.order,a.services,a.contract,a.index,a.money,a.time,a.status,a.statuc,a.static,u.username,m.username as name,m.image,m.c_id,s.name as seller,
        c.trading_name as transaction';
        } else if ($where['services'] == 2) {
            $static = (isset($_GET['static']) && $_GET['static']) ? $_GET['static'] : array('neq', 0);
            if ($static == 1) {
                $where['a.statuc'] = 1;
                $where['a.static'] = 1;
            } else if ($static == 2) {
                $where['a.static'] = 2;
            } else if ($static == 3) {
                $where['a.status'] = 8;
            } else if ($static == 4) {
                $where['a.status'] = array('in', array(10, 11));
            } else if ($static == 5) {
                $where['a.status'] = 14;
            } else if ($static == 6) {
                $where['a.status'] = array('in', array(6, 8));

            }
            $in[0] = "__USER__ u";
            $in[1] = "u.uid = a.username";
            $where['m.classify'] = (isset($_GET['transaction_id']) && !empty($_GET['transaction_id'])) ? $_GET['transaction_id'] : array('neq', 0);
            $where['order|m.goods_name'] = array('like', "%$username%");
            $ji[0] = "__SERVICE_MARKET__ m";
            $ji[1] = "m.id = a.s_id";
            $pn[0] = "__SERVICE_TYPE__ c";
            $pn[1] = "c.id = m.type";
            $join[0] = "__SHOP_INFORMATION__ s";
            $join[1] = "s.id = m.s_id";
            $join[2] = "__USER__ u";
            $join[3] = "u.uid = a.username";
            $field = 'a.id,a.order,a.services,a.contract,a.money,a.time,a.static,a.statuc,a.status,u.username,m.goods_name as name,m.pic as image,s.name as seller,
            m.type as c_id,c.t_name as transaction';
        }

        $res['data'] = db('orders')->order('id desc')->alias('a')->where($where)->field($field)
            ->join($in[0], $in[1])->join($ji[0], $ji[1])->join($pn[0], $pn[1])->join($join[0], $join[1])
            ->limit(10)->page($page)->select();
        if ($where['services'] == 2) {
            $form_list = new order_list();
            $list_name = $form_list->getForm();
            foreach ($list_name as $k => $v) {
                foreach ($res['data'] as $key => $val) {
                    if ($val['statuc'] == $k) {
                        $res['data'][$key]['list_name'] = $v;
                    }
                }
            }
        }
        $res['count'] = db('orders')->alias('a')
            ->join($in[0], $in[1])->join($ji[0], $ji[1])->join($pn[0], $pn[1])->join($join[0], $join[1])->where($where)->count();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '宝贝管理信息查询成功', $res);
        } else {
            $this->_jsonp('1000', '宝贝管理信息查询失败', null);
        }
    }


    /**
     * 宝贝管理
     * 详情页
     */
    public function orders_msg()
    {
        $where['a.id'] = trim(input('get.id'));
        $type = db('orders')->alias('a')->where($where)->find();

        $in[0] = "__USER__ u";
        $in[1] = "u.uid = a.username";


        if ($type['services'] == 1) {
            $ji[0] = "__MARKET_TRADING__ m";
            $ji[1] = "m.id = a.name";
            $pn[0] = "__MARKET_TRADING_CID__ c";
            $pn[1] = "c.id = m.c_id";
            $join[0] = "__SHOP_INFORMATION__ s";
            $join[1] = "s.id = m.name";
            $field = 'a.id,a.index,a.order,a.address_username,a.money,a.time,a.services,a.statuc,a.status,a.static,a.seller_e,u.username,m.username as name,m.image,m.c_id,s.name as seller,
        c.trading_name as transaction';
            $res = db('orders')->alias('a')
                ->join($in[0], $in[1])->join($ji[0], $ji[1])->join($pn[0], $pn[1])->join($join[0], $join[1])
                ->field($field)->where($where)->find();
            $address = db('orders_address')->where('id', $res['address_username'])->find();
            $res['address'] = $address['location'] . "&nbsp" . $address['address'];
        } else if ($type['services'] == 2) {
            $ji[0] = "__SERVICE_MARKET__ m";
            $ji[1] = "m.id = a.s_id";
            $pn[0] = "__SERVICE_TYPE__ c";
            $pn[1] = "c.id = m.type";
            $join[0] = "__SHOP_INFORMATION__ s";
            $join[1] = "s.id = m.s_id";
            if (!empty($type['address_username']) && !empty($type['address_seller'])) {
                $field = 'a.id,a.order,a.money,a.time,a.services,a.username_e,a.seller_e,a.statuc,a.status,a.static,e.location as e_lo,e.address as e_ad,f.location as f_lo,f.address as f_ad,a.address_seller,u.username,m.goods_name as name,m.pic as image,s.name as seller,
            m.type as c_id,c.t_name as transaction';
                $res = db('orders')->alias('a')
                    ->join($in[0], $in[1])->join($ji[0], $ji[1])->join($pn[0], $pn[1])->join($join[0], $join[1])
                    ->join('zgh_orders_address e', 'e.id=a.address_username')
                    ->join('zgh_orders_address f', 'f.id=a.address_seller')
                    ->field($field)->where($where)->find();
            } else {
                $field = 'a.id,a.order,a.money,a.time,a.services,a.username_e,a.seller_e,a.statuc,a.status,a.static,a.address_seller,u.username,m.goods_name as name,m.pic as image,s.name as seller,
            m.type as c_id,c.t_name as transaction';
                $res = db('orders')->alias('a')
                    ->join($in[0], $in[1])->join($ji[0], $ji[1])->join($pn[0], $pn[1])->join($join[0], $join[1])
                    ->field($field)->where($where)->find();
            }


        }

        if ($type['services'] == 2) {
            $form_list = new order_list();
            $list_name = $form_list->getForm();
            foreach ($list_name as $k => $v) {
                if ($res['statuc'] == $k) {
                    $res['list_name'] = $v;
                }
            }
        }
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /**
     * 宝贝管理
     * 查询金额
     */
    public function orders_money()
    {
        $where['id'] = trim(input('get.id'));
        $refund = input('get.refund');

        $field = "seller,money,services";
        $res = db('orders')->where($where)->field($field)->find();
        if (!$refund) {
            $res['alipay'] = db('authentication_personal')->where('uid', $res['seller'])->value('alipay');
            if (!$res['alipay']) {
                $res['alipay'] = db('authentication_enterprise')->where('uid', $res['seller'])->value('alipay');
            }
        }
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }


    /**
     * 宝贝管理
     * refund == refund
     * 交易失败
     * 退款买家
     *
     * 否则
     * 交易成功
     * 打款卖家
     */
    public function orders_refund()
    {
        $where['id'] = trim(input('get.id'));
        $res = Db::name('orders')->where($where)->find();
        $refund = input('get.refund');
        if ($res['services'] == 2) {
            if ($refund == 'refund') {
                $data['static'] = 4;
            } else {
                $data['static'] = 4;
                $data['status'] = 8;
            }
        } else {
            if ($refund == 'refund') {
                $data['static'] = 3;
                $data['status'] = 13;
                $data['statuc'] = 14;
                $data['money'] = 0;
            }
        }
        $where1['id'] = $res['offer_id'];
        $data1['static'] = 4;
        db('offer')->where($where1)->update($data1);

        $zgh_orders = new Data('orders');
        $res = $zgh_orders->_update($data, $where);
        if (isset($res) || !empty($res)) {
            action_log('宝贝管理修改状态', 'log_admin', 1);
            $this->_jsonp('200', '成功', $res);
        } else {
            action_log('宝贝管理修改状态', 'log_admin');
            $this->_jsonp('1000', '失败', null);
        }
    }

//    /**
//     * 宝贝管理
//     * 交易成功
//     * 打款给卖家
//     */
//    public function orders_money_ding()
//    {
//        $where['id'] = trim(input('get.id'));
//        $zgh_orders = new Data('orders');
//        $res = $zgh_orders->_update($data, $where);
//        if (isset($res) || empty($res)) {
//            $this->_jsonp('200', '打款成功', $res);
//        } else {
//            $this->_jsonp('1000', '打款失败', null);
//        }
//    }

    /**
     * 宝贝管理
     * 上传快递单号
     * 商家和用户
     */
    public function orders_express()
    {
        $where['id'] = trim(input('get.id'));
        $data['express_username'] = trim(input('get.express_username'));
        $data['express_seller'] = trim(input('get.express_seller'));
        $data['statuc'] = 7;
        $data['status'] = 7;
        $data1['order_status'] = 7;//上传合同
        $data1['type'] = 2;//上传合同
        $data1['order_num'] = trim(input('get.id'));
        $data1['order_msg'] = '审核已通过,等待双方确认收货';
        $data1['time'] = time();
        Db::name('order_msg')->insert($data1);
        $zgh_orders = new Data('orders');
        $res = $zgh_orders->_update($data, $where);
        if (isset($res) || !empty($res)) {
            action_log('宝贝管理修改状态和上传快递单号', 'log_admin', 1);
            $this->_jsonp('200', '上传成功', $res);
        } else {
            action_log('宝贝管理修改状态和上传快递单号', 'log_admin');
            $this->_jsonp('1000', '上传失败', null);
        }

    }

    /**
     * 显示资源列表
     * 订单评价管理
     * @return \think\Response
     */
    public function evaluation_orders()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();

        //$where['services'] = (isset($_GET['services']) && !empty($_GET['services'])) ? $_GET['services'] : array('neq', 0);
        $where['w.time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $where['w.order_number'] = array('like', "%$username%");
        $where['w.sheet'] = 1;
        $where['w.type'] = 1;
        $where['w.status'] = 1;
        $res['data'] = Db::name('order_comment')->alias('w')->join('zgh_service_market s', 'w.g_id=s.id')->join('zgh_user u', 'w.buyer_id=u.uid')->field('w.*,s.goods_name,s.pic,u.username')->limit(10)->where($where)->page($page)->select();
        $res['count'] = Db::name('order_comment')->alias('w')->join('service_market s', 'w.g_id=s.id')->where($where)->count();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '订单评价管理信息查询成功', $res);
        } else {
            $this->_jsonp('1000', '订单评价管理信息查询失败', null);
        }
    }

    /**
     * 订单评论详情
     */
    public function orders_comment_msg()
    {
        $orders_id = trim(input('get.id'));
        $where['w.id'] = $orders_id;
        $orders = Db::name('order_comment')->alias('w')->where($where)->field('order_number')->find();
        $map['w.order_number'] = $orders['order_number'];
        $map['w.status'] = 1;
        $data = Db::name('order_comment')->alias('w')
            ->join('zgh_user u', 'w.buyer_id=u.uid')
            ->join('zgh_shop_information i', 'w.seller_id=i.uid', 'left')
            ->field('w.*,u.username,u.head_img,i.image')
            ->where($map)->order('w.time asc')->select();
        //屏蔽关键字
        $match = new Preg();
        foreach ($data as $key => $val) {
            $html = $match->replace($val['content']);
            $data[$key]['content'] = $html;
        }
        //
        $res = $data;
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '订单评论获取成功', $res);
        } else {
            $this->_jsonp('1000', '订单评论获取失败', null);
        }
    }

    /**
     * 删除评论
     */
    public function orders_comment_del()
    {
        $id = trim(input('get.id'));
        //db('order_comment')->delete($id);
        $data['status'] = 2;
        $g_id = db('order_comment')->where('id', $id)->where('a_id', 0)->where('sheet', 2)->value('g_id');
        if ($g_id) {
            db('service_market')->where('id', $g_id)->setDec('rate');
        }
        db('order_comment')->where('id', $id)->update($data);
        action_log('订单评论删除评论', 'log_admin', 1);
        $this->_jsonp('200', '删除评论成功', null);
    }


    /**
     * 显示资源列表
     * 地址管理
     * @return \think\Response
     */
    public function address_orders()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();


        $where['a.time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $where['u.username|u.phone'] = array('like', "%$username%");
        $where['a.is_default'] = array('in', array(1, 2));

        $join[0] = "__USER__ u";
        $join[1] = "u.uid = a.username";

        $field = "u.username,a.time,a.id,a.phone,a.nick,a.address,a.code,a.location,a.time";
        $res['data'] = db('orders_address')->alias('a')->field($field)->
        join($join[0], $join[1])->where($where)->limit(10)->page($page)->select();
        $res['count'] = db('orders_address')->alias('a')->field($field)->join($join[0], $join[1])->where($where)->count();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '地址管理信息查询成功', $res);
        } else {
            $this->_jsonp('1000', '地址管理信息查询失败', null);
        }
    }

    /**
     * 显示资源列表
     * 订单评价管理
     * @return \think\Response
     */
    public function details_orders()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);

        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();


        $where['services'] = (isset($_GET['services']) && !empty($_GET['services'])) ? $_GET['services'] : array('neq', 0);
        $where['static'] = (isset($_GET['static']) && !empty($_GET['static'])) ? $_GET['static'] : array('neq', 0);
        $where['time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $where['order|name'] = array('like', "%$username%");

        $zgh_orders = new Data('order_details');
        $res['data'] = $zgh_orders->selectPage($page, $where);
        $res['count'] = $zgh_orders->count($where);
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '订单明细管理信息查询成功', $res);
        } else {
            $this->_jsonp('1000', '订单明细管理信息查询失败', null);
        }
    }

    public function download()
    {
        $where['id'] = $_GET['id'];
        $type = $_GET['type'];
        if ($type == 1) {
            $dbname = 'complain';
            $value = 'pic';
        } else if ($type == 2) {
            $dbname = 'orders';
            $value = 'contract';
        }
        $res = db($dbname)->where($where)->value($value);
        $pic = json_decode($res, true);


        $zip = new \ZipArchive;

        $time = time();
        //压缩文件名
        $filename = "download$time.zip";
        //新建zip压缩包
        $zip->open('public' . "Images/download/" . $filename, \ZipArchive::OVERWRITE);
        //把图片一张一张加进去压缩

        foreach ($pic as $value) {
            if ($type == 1) {
                $zip->addFile("Images/appeal/$value");
            } else if ($type == 2) {
                $zip->addFile("Images/contract/$value", "zgh/$value");
            }

        }

        //打包zip
        $zip->close();

        //或者输出下载
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header('Content-disposition: attachment; filename=' . basename($filename)); //文件名
        header("Content-Type: application/force-download");
        header("Content-Transfer-Encoding: binary");
        header('Content-Length: ' . filesize("Images/download/$filename")); //告诉浏览器，文件大小
        readfile("Images/download/$filename");

    }

}
